package com.scedu.service.impl;

import com.scedu.domain.ProductInfo;
import com.scedu.dto.CartDTO;
import com.scedu.enums.ProductStatusEnums;
import com.scedu.enums.ResultEnums;
import com.scedu.exception.SellException;
import com.scedu.repository.ProductInfoRepository;
import com.scedu.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/*
商品信息服务实现类
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {
    @Autowired
    ProductInfoRepository productInfoRepository;
    @Override
    public ProductInfo findone(String productInfoId) {
        return productInfoRepository.findById(productInfoId).orElse(null);
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
       return productInfoRepository.findAll(pageable);

    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return productInfoRepository.save(productInfo);
    }

    @Override
    public List<ProductInfo> findByProductStatus(Integer productStatus) {
        return productInfoRepository.findByProductStatus(productStatus);
    }

    @Override
    public List<ProductInfo> findOnlineAll() {
        return productInfoRepository.findByProductStatus(ProductStatusEnums.ONLINE.getCode());
    }

    @Override
    public List<ProductInfo> findOfflineAll() {
        return productInfoRepository.findByProductStatus(ProductStatusEnums.OFFLINE.getCode());
    }

    @Override
    public List<ProductInfo> findByCategoryType(Integer categoryType) {
        return productInfoRepository.findByCategoryType(categoryType);
    }

    @Override
    @Transactional
    public void inreaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO :cartDTOList){
            ProductInfo productInfo=productInfoRepository.getOne(cartDTO.getProductId());
            if (productInfo==null){
                //如果商品信息为空，抛出异常
                throw  new SellException(ResultEnums.PRODUCT_NOT_EXIT_ERROR);
            }
            productInfo.setProductStock(productInfo.getProductStock()+cartDTO.getProductQuantity());
            productInfoRepository.save(productInfo);
        }
    }

    @Override
    @Transactional
    public void dereaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO :cartDTOList){
            ProductInfo productInfo=productInfoRepository.getOne(cartDTO.getProductId());
            if (productInfo==null){
                //如果商品信息为空，抛出异常
                throw  new SellException(ResultEnums.PRODUCT_NOT_EXIT_ERROR);
            }
            Integer result=productInfo.getProductStock()-cartDTO.getProductQuantity();
            if (result<0){
                throw new SellException(ResultEnums.PRODUCT_NOT_ENOUGH_ERROT);
            }
            productInfo.setProductStock(result);


            productInfoRepository.save(productInfo);
        }

    }

    @Override
    @Transactional
    public ProductInfo onsale(String productId) {
        ProductInfo productInfo=productInfoRepository.getOne(productId);
        if (productInfo==null){
            //如果商品信息为空，抛出异常
            throw  new SellException(ResultEnums.PRODUCT_NOT_EXIT_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnums.ONLINE.getCode());
        productInfoRepository.save(productInfo);
        return  productInfo;
    }

    @Override
    @Transactional
    public ProductInfo downsale(String productId) {
        ProductInfo productInfo=productInfoRepository.getOne(productId);
        if (productInfo==null){
            //如果商品信息为空，抛出异常
            throw  new SellException(ResultEnums.PRODUCT_NOT_EXIT_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnums.OFFLINE.getCode());
        productInfoRepository.save(productInfo);
        return  productInfo;
    }




}
